package cn.yunhe.action;

import cn.yunhe.pojo.Book;
import cn.yunhe.service.BookService;
import cn.yunhe.util.BaseAction;
import cn.yunhe.util.UploadFileUtil;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import java.io.*;

public class UpdateBookAction extends BaseAction implements ModelDriven {
    private Book book;

    /*private File upload;
    private String uploadContentType;
    private String uploadFileName;

    private String uploadSavePath;*/
    private UploadFileUtil uploadFileUtil;


    BookService bookService=new BookService();
    public String updateBook() throws IOException {
        System.out.println("<====book.getBname()======>"+book.getBname());
        System.out.println("<====book.getBpic()====1==>"+book.getBpic());

        /*String uploadFinalSavePath=getUploadSavePath()+"\\"+getUploadFileName();
        doupload(upload,uploadFinalSavePath);

        book.setBpic(getUploadFileName());*/

        System.out.println("uploadFileUtil.getFinalSavePath()===="+uploadFileUtil.getFinalSavePath());

        uploadFileUtil.doUpload(uploadFileUtil.getUpload(),uploadFileUtil.getFinalSavePath());

        book.setBpic(uploadFileUtil.getUploadFileName());

        System.out.println("<====book.getBpic()===2===>"+book.getBpic());
        boolean flag=bookService.updateBook(book);
        if (flag){
            return SUCCESS;
        }else {
            return null;
        }
    }



    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public UploadFileUtil getUploadFileUtil() {
        /*return uploadFileUtil;*/
        if (uploadFileUtil==null){
            uploadFileUtil=new UploadFileUtil();
        }
        return uploadFileUtil;
    }

    public void setUploadFileUtil(UploadFileUtil uploadFileUtil) {
        this.uploadFileUtil = uploadFileUtil;
    }

    @Override
    public Book getModel() {
        if (book==null){
            book=new Book();
        }
        return book;
    }

}
